package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.InviteUploadInfo;
import com.yy.jhl.interfaces.CommonCallBack;
import com.yy.jhl.ui.manager.adapter.UploadAdapter;
import com.yy.jhl.ui.manager.contract.UploadHistoryContract;
import com.yy.jhl.ui.manager.presenter.UploadHistoryPresenter;
import com.yy.jhl.ui.qrcode.QRScanActivity;
import com.yy.jhl.util.DialogUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/10.
 */

public class UploadHistoryActivity extends BaseActivity<UploadHistoryPresenter> implements UploadHistoryContract.View, AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2 {

    UploadAdapter uploadAdapter;
    TextView orderNameText;
    TextView orderCodeText;
    TextView orderGysText;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.upload_btn)
    Button continueBtn;
    String completed = "";
    @BindView(R.id.no_data_view)
    LinearLayout noDataView;
    boolean isShowDialog = true;
    boolean isFromScan = false;
    public static void startAction(Context mContext, String completed, boolean isFromScan) {
        Intent intent = new Intent(mContext, UploadHistoryActivity.class);
        intent.putExtra("completed", completed);
        intent.putExtra("isFromScan", isFromScan);
        mContext.startActivity(intent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        completed = intent.getStringExtra("completed");
        isFromScan = intent.getBooleanExtra("isFromScan",false);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_upload_history;
    }

    private void addHeadView() {
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.header_act_upload_history, null);
        orderNameText = (TextView) headerView.findViewById(R.id.order_name_text);
        orderCodeText = (TextView) headerView.findViewById(R.id.order_code_text);
        orderGysText = (TextView) headerView.findViewById(R.id.order_gys_text);
        listview.addHeaderView(headerView);
        listview.setVisibility(View.GONE);
    }

    @Override
    public void initView() {
        completed = getIntent().getStringExtra("completed");
        isFromScan = getIntent().getBooleanExtra("isFromScan",false);
        addHeadView();
        showTitle("上传记录", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        boolean isEdit = true;
        if (!isFromScan && "1".equals(completed)) {
            isEdit = false;
            continueBtn.setVisibility(View.GONE);
        }
        uploadAdapter = new UploadAdapter(mContext, isEdit);
        listview.setAdapter(uploadAdapter);
        mPresenter.getHistoryInfo(isShowDialog);
        isShowDialog = false;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void getHistoryInfoSuccess(List<InviteUploadInfo> infos) {
        listview.setVisibility(View.VISIBLE);
        if (infos != null && infos.size() > 0) {
            InviteUploadInfo firstInfo = infos.get(0);
            orderNameText.setText(firstInfo.getTitle());
            orderCodeText.setText(firstInfo.getOrder_sn());
            orderGysText.setText(firstInfo.getScompany_name());
            uploadAdapter.addAllData(infos);
            if (isFromScan && "1".equals(completed)) {
                DialogUtil.showEnsureDialogView(mContext, "您的封箱已上传完成？\n点击确定后将不能进行修改", new CommonCallBack() {
                    @Override
                    public void getCallBack() {
                        mPresenter.checkInfo();
                    }
                });
            }
        }else {
            noDataView.setVisibility(View.VISIBLE);
            listview.setVisibility(View.GONE);
        }
    }

    @Override
    public void getHistoryInfoFail() {

    }

    @Override
    public void getCheckInfo(boolean isSuccess) {
        if (isSuccess) {
            uploadAdapter.setEdit(false);
            continueBtn.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.upload_btn)
    public void onViewClicked() {
        startActivityForResult(QRScanActivity.class, 1000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            String result = data.getStringExtra(QRScanActivity.EXTRA_QRCODE_SUCCESS);
            switch (requestCode) {
                case 1000:
                    if (result.equals("***")) {
                        showShortToast("扫描结果格式有误！");
                    } else {
                        InviteBoxInfoActivity.startAction(mContext, result);
                    }
                    break;
            }
        }
    }
}
